<template>
  <div class="h-full">
    <BaseTable
      :loading="loading || pageLoading"
      class="table-style"
      column-code="000958"
      max-fill
      :data-source="dataSource"
      :scroll="{ x: 1400 }"
      :expanded-row-keys="expandedRowKeys"
    >
      <template #bodyCell="{ column, index, record }">
        <template v-if="column.dataIndex === 'xh'">
          {{ index + 1 }}
        </template>
        <template v-if="column.dataIndex === 'link'">
          <div class="table-link"></div>
        </template>
        <template v-if="column.dataIndex === 'status'">
          <a-tag :color="orderStatusEnum[record.status]?.color">{{ orderStatusEnum[record.status]?.name }}</a-tag>
        </template>
        <template v-if="column.dataIndex === 'advExecType'">
          <!-- 长期/临时 -->
          {{ ['临时', '长期', '出院带药'][record.advExecType] || '' }}
        </template>
        <template v-if="column.title === '处置内容'">
          <PreviewCell :row-data="record" />
        </template>
        <template v-if="column.title === '开始时间'">
          {{ record.startTime || '-' }}
        </template>
        <template v-if="column.dataIndex === 'stopTime'">
          <div v-if="record.advExecType === 0">{{ record.endTime || '-' }}</div>
          <div v-if="record.advExecType === 1">{{ record.stopTime || '-' }}</div>
        </template>
        <template v-else-if="column.dataIndex === 'action'">
          <a-button type="link" size="small" @click="onClickTime(record)"> 调整 </a-button>
        </template>
      </template>
    </BaseTable>
    <AdjustTime v-model:open="timeOpen" :row-data="rowData" @success="getData" />
  </div>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import PreviewCell from '@/views/in-doctor/workstation/components/TabDoctorAdvice/PreviewCell'
import { post } from '@/utils/request'
// import { getSystemConfig } from '@/utils/systemConfig'
import useStore from '../../store'
import AdjustTime from './AdjustTime'

defineProps({
  pageLoading: {
    type: Boolean,
    default: false
  }
})

const orderStatusEnum = {
  0: { name: '开立', value: 0, color: 'blue' },
  1: { name: '已签署', value: 1, color: 'success' },
  2: { name: '已签收', value: 2, color: 'success' },
  3: { name: '已申请', value: 3, color: 'success' },
  4: { name: '待执行', value: 4, color: 'success' },
  5: { name: '已执行', value: 5, color: 'success' },
  6: { name: '已停止', value: 6, color: 'red' },
  7: { name: '已作废', value: 7, color: 'red' },
  8: { name: '执行中', value: 8, color: 'blue' },
  9: { name: '已发药', value: 9, color: 'success' },
  10: { name: '退药中', value: 10, color: 'red' },
  11: { name: '已退药', value: 11, color: 'success' }
}
const store = useStore()
const loading = ref(true)
const timeOpen = ref(false)
const expandedRowKeys = ref(['row-0'])
const dataSource = ref([])

const rowData = ref({})

//获取数据
const getData = async () => {
  if (!store.currentPatient.inplsh) {
    dataSource.value = []
    loading.value = false
    return
  }

  loading.value = true
  const res = await post('/inp/zyys/getClinicalOrderInfoList', {
    isDisplayed: '1',
    inpAdvice: '', //医嘱执行类别
    zjm: '',
    inpAdviceType: '0', //0住院医嘱 1术后医嘱
    inplsh: store.currentPatient.inplsh
  })
  loading.value = false
  if (res.code !== 0) return message.error(res.msg || '获取数据失败')
  if (res.data.length) {
    dataSource.value = res.data.map((item) => {
      item.key = item.id
      if (item.children?.length) {
        expandedRowKeys.value.push(item.id)
        item.children = item.children.map((child) => ({ ...child, key: child.id, isChild: true }))
      }
      return item
    })
  } else {
    dataSource.value = []
  }
}

const onClickTime = (row) => {
  timeOpen.value = true
  rowData.value = { ...row }
}

watch(
  () => store.currentPatient?.inplsh,
  () => {
    getData()
    rowData.value = {}
  },
  { immediate: true }
)
</script>
<style lang="less" scoped>
.table-header {
  background: var(--thin-color);
  &:first-child {
    margin-top: 0;
  }
}

:deep(.ant-table-row-expand-icon) {
  display: none;
}
:deep(.ant-table-row-indent) {
  display: none;
}
</style>
